package com.xudow.app.util;

import android.content.Context;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

/**
 * ImageUtils
 *
 * @author lukecheng
 */
public class ImageUtils {

    /*
    public static void loadImage(Context context, final ImageView imageView, final String url, int defImageRes) {
        if (StringUtils.isEmpty(url)) {
            imageView.setImageResource(defImageRes);
            return;
        }

        String nullImage = "?name=null";
        if (url.indexOf(nullImage) == (url.length() - nullImage.length())) {
            imageView.setImageResource(defImageRes);
            return;
        }


        final ImageAware imageAware = new ImageViewAware(imageView, false);
        ImageLoader.getInstance().loadImage(url, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);

                imageAware.setImageBitmap(loadedImage);
            }
        });
    } */

    public static void loadImage(Context context, final ImageView imageView, final String url, int defImageRes) {
        if (null == imageView) {
            return;
        }

        if (StringUtils.isEmpty(url)) {
            imageView.setImageResource(defImageRes);
            return;
        }

        String nullImage = "?name=null";
        if (url.indexOf(nullImage) == (url.length() - nullImage.length())) {
            imageView.setImageResource(defImageRes);
            return;
        }

        Picasso.with(context)
                .load(url)
                .placeholder(defImageRes)
                .error(defImageRes)
                .into(imageView);
    }

    public static void loadImageWithLock(Context context, final ImageView imageView,  int defImageRes) {
        String url=(String)(imageView.getTag());
        if (null == imageView) {
            return;
        }

        if (StringUtils.isEmpty(url)) {
            imageView.setImageResource(defImageRes);
            return;
        }

        String nullImage = "?name=null";
        if (url.indexOf(nullImage) == (url.length() - nullImage.length())) {
            imageView.setImageResource(defImageRes);
            return;
        }

        Picasso.with(context)
                .load(url)
                .placeholder(defImageRes)
                .error(defImageRes)
                .into(imageView);
    }
}
